package com.ajou.adonate.imap;


import java.util.Properties;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;

public class UserSession {
	
	public String m_sUserID = null;
	public String m_sUserPW = null;
	public Session m_Session = null;
	public Store m_Store = null;
	public Folder m_Folder = null;
	public Boolean m_IsConnect;
	private int m_DB_MemberId = -1;
	
	public UserSession() {
		m_IsConnect = false;		
	}
	
	public void setDB_MemberId(int m_DB_MemberId) {
		this.m_DB_MemberId = m_DB_MemberId;
	}

	public int getDB_MemberId() {
		return m_DB_MemberId;
	}

	public void CloseSession() {
		try {
			m_Folder.close(false);
			m_Store.close();
		} catch (MessagingException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public String getID() {
		return m_sUserID;
	}
	
	public String getPassword() {
		return m_sUserPW;
	}
	
	public Session getSession() {
		return m_Session;
	}
	
	public Store getStore() {
		if(m_Store.isConnected() != true)
			if (ConnectSession(m_sUserID, m_sUserPW) == true)
				return m_Store;
		
		return m_Store;		
	}
	
	public Folder getFolder() {
		return m_Folder;
	}
	
   	public Boolean ConnectSession(String id, String password) {
   	    Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", "imaps");
        props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.imap.socketFactory.fallback", "false");
        //props.setProperty("mail.mime.base64.ignoreerrors", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.required", "true");
        props.put("mail.imap.fetchsize", "1024000"); 
        
        try {
        	m_Session = Session.getDefaultInstance(props, null);
            
        	m_Store = m_Session.getStore("imaps");
        	m_Store.connect("imap.gmail.com", id, password);
            
            if(m_Store.isConnected() == true) {
            	System.out.println("Success Login " + id);
            	m_sUserID = id;
            	m_sUserPW = password;
            	m_IsConnect = true;
            	
            	m_Folder = m_Store.getFolder("adonate");
        		if (!m_Folder.exists()) {
        			boolean isCreated = m_Folder.create(Folder.HOLDS_FOLDERS
        					| Folder.HOLDS_MESSAGES);
        			System.out.println("make Folder : adonate result : " + isCreated);
        		}
        		System.out.println("folder Open!");
            }

          /*  
            Folder inbox = m_Store.getFolder("Inbox");
            inbox.open(Folder.READ_ONLY);
            Message messages[] = inbox.getMessages();
            for(Message message:messages) {
            	System.out.println(" >> " + message + "//" + message.getSubject());
            }
          */
       } catch (NoSuchProviderException e) {
            e.printStackTrace();
            System.exit(1);
       } catch (MessagingException e) {
    	   System.out.println("UserSession LOGIN invalid user or password");
    	   	m_IsConnect = false;	
    		m_Store = null;
        	m_Session = null;
        	m_sUserID = null;
        	m_sUserPW = null;
            return false;
       }
       
       return true;
	}
}